#!/usr/bin/python
import os,sys,re

def getbasename(filename):
	match=re.match(r'^(.+)\.tar\.(gz|bz2)$',filename)
	if match==None:
		raise Exception('Unknown filename format '+filename)
	return match.group(1)

if os.path.exists('/source')==False: raise Exception('/source not found.')

for filename in os.listdir('/source'):
	if re.match(r'^(.+)\.tar\.(gz|bz2)$',filename): # tar.gz tar.bz2
		print 'check',filename
		basename=os.popen('tar tf '+os.path.join('/source',filename)+' 2>/dev/null', 'r').readline().strip()
		match=re.match('^([^/]+)(/.*)?$', basename)
		if match==None: 
			match=re.match('^([^/]+)$', basename)
			if match==None: raise Exception('Cannot handle output of tar', basename)
		basename=match.group(1)
		if basename!=getbasename(filename): 
			print ('%s\'s content mismatches its filename.'%filename, basename)
